package Dao.DaoImpl;

import Dao.ChangeDormAppDao;
import Utils.JDBCUtils;
import domain.ChangeDormApp;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class ChangeDormAppDaoImpl implements ChangeDormAppDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
    @Override
    public List<ChangeDormApp> showApply() {
        String sql ="select * from changedormapp where checkstate='false'";

        return template.query(sql,new BeanPropertyRowMapper<>(ChangeDormApp.class));
    }

    @Override
    public void changeApply(int id, String state,String checkPeo) {
        String sql ="update changedormapp set checkstate=?,checkPeo=? where id=?";
        template.update(sql,state,checkPeo,id);
    }

    @Override
    public ChangeDormApp selectApplyById(String id) {
        String sql ="select * from changedormapp where id=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(ChangeDormApp.class),id);
    }

    @Override
    public void changeDorm(String stunum, String name, String dormnum) {

    }

    @Override
    public void addChangeDormApp(ChangeDormApp app) {
        String sql = "insert into changedormapp values(null,?,?,?,?,?,?,?,?,null)";
        template.update(sql,app.getStunum(),app.getName(),app.getDormnum(),app.getChangestunum(),app.getChangename(),app.getChangedormnum(),app.getAppReason(),app.getCheckstate());
    }

    @Override
    public ChangeDormApp selectChangeDormAppById(int id) {
        String sql ="select * form changedormapp where id=?";
        return template.queryForObject(sql,new BeanPropertyRowMapper<>(ChangeDormApp.class),id);
    }
}
